package com.ht.module.file.controller;

import com.ht.config.log.annotation.SysLog;
import com.ht.config.minio.service.MinioService;
import com.ht.constant.DicConstants;
import com.ht.module.file.dto.OperationFileDto;
import io.swagger.annotations.ApiOperation;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.ht.module.file.entity.FileInfo;
import com.ht.module.file.service.FileInfoService;
import com.ht.util.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.validation.Valid;


/**
 * <p>
 * 文件信息表 前端控制器
 * </p>
 *
 * @author hejialun
 * @since 2024-06-12
 */
@RestController
@RequestMapping("/file-info")
public class FileInfoController {
    @Resource
    private FileInfoService fileInfoService;
    @Resource
    private MinioService minioService;

    @ApiOperation("上传文件")
    @SysLog(msg = "上传文件", type = DicConstants.OperLogType.UPLOAD)
    @PostMapping("/uploadFile")
    public JsonResult uploadFile(@RequestParam("file") MultipartFile file, FileInfo en) {
        return JsonResult.success(fileInfoService.uploadFile(file, en));
    }

    @ApiOperation("下载文件")
    @SysLog(msg = "下载文件", type = DicConstants.OperLogType.DOWNLOAD)
    @GetMapping("/downloadFile/{fileId}")
    public void downloadFile(@PathVariable("fileId") String fileId) {
        fileInfoService.downloadFileByFileId(fileId);
    }

    @ApiOperation("删除权限范围内的文件")
    @SysLog(msg = "删除权限范围内的文件", type = DicConstants.OperLogType.DEL)
    @DeleteMapping("/delFile/{id}")
    public JsonResult delFileByFileId(@PathVariable String id) {
        fileInfoService.delFileByFileId(id);
        return JsonResult.success();
    }

    @ApiOperation("修改文件信息")
    @SysLog(msg = "修改文件信息", type = DicConstants.OperLogType.UPDATE)
    @PutMapping("/update")
    public JsonResult updateById(@RequestBody @Valid FileInfo en) {
        CommMethod.beanUpdate(en);
        fileInfoService.updateById(en);
        return JsonResult.success();
    }


    /**
     * 预览文件
     */
    @GetMapping("/viewFile/{fileId}")
    public ModelAndView viewFile(@PathVariable("fileId") String fileId, Model model) {
        return fileInfoService.viewFile(fileId, model);
    }


    /**
     * 编辑文件
     */
    @GetMapping("/editFile/{fileId}")
    @SysLog(msg = "编辑文件", type = DicConstants.OperLogType.UPDATE)
    public ModelAndView editFile(@PathVariable("fileId") String fileId, Model model) {
        return fileInfoService.editFile(fileId, model);
    }

    /**
     * 解压解压包
     */
    @PostMapping("/decompression")
    @SysLog(msg = "解压压缩包", type = DicConstants.OperLogType.DECOMPRESSION)
    public JsonResult decompression(@RequestBody @Valid OperationFileDto en) {
        fileInfoService.decompression(en);
        return JsonResult.success();
    }

    @ApiOperation("浏览器预览资源")
    @GetMapping("/viewFileByWindow/{fileId}")
    public void viewFileByWindow(@PathVariable("fileId") String fileId) {
        fileInfoService.viewFileByWindow(fileId);
    }

    @ApiOperation("复制文件")
    @SysLog(msg = "复制文件", type = DicConstants.OperLogType.COPY)
    @PostMapping("/copyFile")
    public JsonResult copyFile(@RequestBody @Valid OperationFileDto en) {
        return JsonResult.success(fileInfoService.copyFile(en));
    }


    @ApiOperation("文件转pdf")
    @GetMapping("/fileToPdf/{fileId}")
    @SysLog(msg = "文件转pdf", type = DicConstants.OperLogType.CONVERT)
    public JsonResult fileToPdf(@PathVariable("fileId") String fileId) {
        return JsonResult.success(fileInfoService.fileToPdf(fileId));
    }

    @ApiOperation("移动文件")
    @PostMapping("/moveFile")
    public JsonResult moveFile(@RequestBody @Valid OperationFileDto en) {
        return JsonResult.success(fileInfoService.moveFile(en));
    }



}
